﻿$(function () {
    $("#btnAddKeywordType1").click(function () { addKeywordType1(); });
    $("#btnAddKeywordType2").click(function () { addKeywordType2(); });
    $("#btnAddKeyword").click(function () { addKeyword(); });
    $("#btnSaveKeywordType1").click(function () { saveKeywordType1(); });
    $("#btnSaveKeywordType2").click(function () { saveKeywordType2(); });
    $("#btnSaveKeyword").click(function () { saveKeyword(); });    
    loadKeywordType1Tables();    
});

//加载列表数据：网站类型
function loadKeywordType1Tables() {
    $("#tableBody").html("");   
    $.ajax({
        type: "GET",
        url: "/Webmonitor/KeywordType1/GetAllListToJson?_t=" + new Date().getTime(),        
        success: function (data) {            
            $.each(data, function (i, item) {
                var tr = "<tr>";                
                tr += "<td>" + item.id + "</td>";
                tr += "<td>" + item.keywordType1Name + "</td>";                
                tr += "<td>" + item.sortIndex + "</td>";
                tr += "<td>" + item.remarks + "</td>";
                tr += "<td><button class='btn btn-info btn-xs' href='javascript:;' onclick='editKeywordType1(\"" + item.id + "\")'><i class='fa fa-edit'></i>  </button> <button class='btn btn-danger btn-xs' href='javascript:;' onclick='deleteKeywordType1(\"" + item.id + "\")'><i class='fa fa-trash-o'></i>  </button> <button class='btn btn-info btn-xs' href='javascript:;' onclick='loadKeywordType2Tables(\"" + item.id + "\")'><i class='fa fa-add'></i> 下级关键字类型 </button> </td>"
                tr += "</tr>";
                $("#tableBody").append(tr);
            }) 
        }
    })
}

//新增
function addKeywordType1() {
    $("#Id").val(0);
    $("#Remarks").val("");
    $("#SortIndex").val(0);

    $("#KeywordType1Name").val("");   

    $("#Title").text("新增一级关键词类型");
    //弹出新增窗体
    $("#editModalKeywordType1").modal("show");
};
//编辑
function editKeywordType1(id) {
    $.ajax({
        type: "Get",
        url: "/Webmonitor/KeywordType1/GetByIdToJson?id=" + id + "&_t=" + new Date().getTime(),
        success: function (data) {
            $("#Id").val(data.id);
            $("#SortIndex").val(data.sortIndex);
            $("#Remarks").val(data.remarks);
            $("#CreatedBy").val(data.createdBy);
            $("#CreatedOn").val(data.createdOn);

            $("#KeywordType1Name").val(data.keywordType1Name);           
           
            $("#Title").text("编辑类型")
            $("#editModalKeywordType1").modal("show");
        }
    })
};
//保存
function saveKeywordType1() {
    
    var postData = {        
        "Id": $("#Id").val(),
        "Remarks": $("#Remarks").val(),
        "SortIndex": $("#SortIndex").val(),
        "CreatedBy": $("#CreatedBy").val(),
        "CreatedOn": $("#CreatedOn").val(),

        "KeywordType1Name": $("#KeywordType1Name").val(),        
    };
    $.ajax({
        type: "Post",
        url: "/Webmonitor/KeywordType1/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {
                loadKeywordType1Tables();
                loadKeywordType2Tables($("#Id").val());
                $("#editModalKeywordType1").modal("hide");
            } else {
                layer.tips(data.message, "#btnSaveKeywordType1");
            };
        }
    });
};
//删除
function deleteKeywordType1(id) {
    layer.confirm("您确认删除选定的记录吗？", {
        btn: ["确定", "取消"]
    },
        function () {
        $.ajax({
            type: "POST",
            url: "/Webmonitor/KeywordType1/DeleteById",
            data: { "id": id },
            success: function (data) {
                if (data.result == "Success") {
                    loadKeywordType1Tables();                    
                    layer.closeAll();
                }
                else {
                    layer.alert(data.message);
                }
            }
        })
    });
};

//加载列表数据：根据上级Id
function loadKeywordType2Tables(id) {
    $("#tableBody1").html(""); 
    $("#KeywordType1Id").val(id);
    $.ajax({
        type: "GET",
        url: "/Webmonitor/KeywordType2/GetAllListByKeywordType1ToJson?id=" + id + "&_t=" + new Date().getTime(),
        success: function (data) {            
            $.each(data, function (i, item) {
                var tr = "<tr>";
                tr += "<td>" + item.id + "</td>";
                tr += "<td>" + item.keywordType2Name + "</td>";
                tr += "<td>" + item.sortIndex + "</td>";
                tr += "<td>" + item.remarks + "</td>";
                tr += "<td><button class='btn btn-info btn-xs' href='javascript:;' onclick='editKeywordType2(\"" + item.id + "\")'><i class='fa fa-edit'></i>  </button> <button class='btn btn-danger btn-xs' href='javascript:;' onclick='deleteKeywordType2(\"" + item.id + "\")'><i class='fa fa-trash-o'></i>  </button>  <button class='btn btn-info btn-xs' href='javascript:;' onclick='loadKeywordTables(\"" + item.id + "\")'><i class='fa fa-add'></i> 下级关键字 </button> </td></td>"
                tr += "</tr>";
                $("#tableBody1").append(tr);
            });
            loadKeywordTables(0);
        }
    })
}

//新增
function addKeywordType2() {
    if ($("#KeywordType1Id").val() == "") {
        alert("关键词一级类型为空");
        return;
    }        
    $("#KeywordType1Id1").val($("#KeywordType1Id").val());
    $("#Id1").val(0);
    $("#Remarks1").val("");
    $("#SortIndex1").val(0);

    $("#KeywordType2Name").val("");   

    $("#Title1").text($("#KeywordType1Id").val()+"-新增二级关键词");
    //弹出新增窗体
    $("#editModalKeywordType2").modal("show");
};

//编辑
function editKeywordType2(id) {
    $.ajax({
        type: "Get",
        url: "/Webmonitor/KeywordType2/GetByIdToJson?id=" + id + "&_t=" + new Date().getTime(),
        success: function (data) {
            $("#Id1").val(data.id);
            $("#SortIndex1").val(data.sortIndex);
            $("#Remarks1").val(data.remarks);
            $("#CreatedBy1").val(data.createdBy);
            $("#CreatedOn1").val(data.createdOn);

            $("#KeywordType1Id1").val(data.keywordType1Id);
            $("#KeywordType2Name").val(data.keywordType2Name);           

            $("#Title1").text("编辑一级关键词")
            $("#editModalKeywordType2").modal("show");
        }
    })
};

//保存
function saveKeywordType2() {
    var postData = {
        "Id": $("#Id1").val(),
        "Remarks": $("#Remarks1").val(),
        "SortIndex": $("#SortIndex1").val(),
        "CreatedBy": $("#CreatedBy1").val(),
        "CreatedOn": $("#CreatedOn1").val(),

        "KeywordType1Id": $("#KeywordType1Id1").val(),
        "KeywordType2Name": $("#KeywordType2Name").val()        
    };
    $.ajax({
        type: "Post",
        url: "/Webmonitor/KeywordType2/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {
                loadKeywordType2Tables($("#KeywordType1Id1").val());
                loadKeywordTables(0);
                $("#editModalKeywordType2").modal("hide");
            } else {
                layer.tips(data.message, "#btnSaveKeywordType2");
            };
        }
    });
};

//删除
function deleteKeywordType2(id) {
    layer.confirm("您确认删除选定的记录吗？", {
        btn: ["确定", "取消"]
    }, function () {
        $.ajax({
            type: "POST",
            url: "/Webmonitor/KeywordType2/DeleteById",
            data: { "id": id },
            success: function (data) {
                if (data.result == "Success") {
                    loadKeywordType2Tables($("#KeywordType1Id").val());
                    layer.closeAll();
                }
                else {
                    layer.alert(data.message);
                }
            }
        })
    });
};

//加载列表数据
function loadKeywordTables(id) {
    $("#tableBody2").html("");    
    $("#KeywordType2Id").val(id);
    $.ajax({
        type: "GET",
        url: "/Webmonitor/Keyword/GetAllListByKeywordType2ToJson?id=" + id + "&_t=" + new Date().getTime(),
        success: function (data) {
            $.each(data, function (i, item) {
                var tr = "<tr>";
                tr += "<td>" + item.id + "</td>";
                tr += "<td>" + item.keywordName + "</td>";                 
                tr += "<td>" + item.sortIndex + "</td>";
                tr += "<td>" + item.remarks + "</td>";
                tr += "<td><button class='btn btn-info btn-xs' href='javascript:;' onclick='editKeyword(\"" + item.id + "\")'><i class='fa fa-edit'></i>  </button> <button class='btn btn-danger btn-xs' href='javascript:;' onclick='deleteKeyword(\"" + item.id + "\")'><i class='fa fa-trash-o'></i>  </button> </td>"
                tr += "</tr>";
                $("#tableBody2").append(tr);
            })
        }
    })
}

//新增
function addKeyword() {
    if ($("#KeywordType1Id").val() == "") {
        alert("一级关键词类型为空");
        return;
    } 
    else if ($("#KeywordType2Id").val() == 0) {
        alert("二级关键词类型为空");
        return;
    } 
    $("#KeywordType1Id1").val($("#KeywordType1Id").val());
    $("#KeywordType2Id2").val($("#KeywordType2Id").val());
    $("#Id2").val(0);
    $("#Remarks2").val("");
    $("#SortIndex2").val(0);

    $("#KeywordType2Name").val("");    

    $("#Title2").text($("#KeywordType1Id").val()+"-" +$("#KeywordType2Id").val()+"-新增关键词");
    //弹出新增窗体
    $("#editModalKeyword").modal("show");
};

//编辑
function editKeyword(id) {
    $.ajax({
        type: "Get",
        url: "/Webmonitor/Keyword/GetByIdToJson?id=" + id + "&_t=" + new Date().getTime(),
        success: function (data) {
            $("#Id2").val(data.id);
            $("#SortIndex2").val(data.sortIndex);
            $("#Remarks2").val(data.remarks);
            $("#CreatedBy2").val(data.createdBy);
            $("#CreatedOn2").val(data.createdOn);

            $("#KeywordType1Id2").val(data.keywordType1Id);
            $("#KeywordType2Id2").val(data.keywordType2Id);
            $("#KeywordName").val(data.keywordName);            

            $("#Title2").text("编辑关键词")
            $("#editModalKeyword").modal("show");
        }
    })
};

//保存
function saveKeyword() {
    var postData = {
        "Id": $("#Id2").val(),
        "Remarks": $("#Remarks2").val(),
        "SortIndex": $("#SortIndex2").val(),
        "CreatedBy": $("#CreatedBy2").val(),
        "CreatedOn": $("#CreatedOn2").val(),

        "KeywordType1Id": $("#KeywordType1Id2").val(),
        "KeywordType2Id": $("#KeywordType2Id2").val(),
        "KeywordName": $("#KeywordName").val()
    };
    $.ajax({
        type: "Post",
        url: "/Webmonitor/Keyword/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {
                loadKeywordTables($("#KeywordType2Id2").val())
                $("#editModalKeyword").modal("hide");
            } else {
                layer.tips(data.message, "#btnSaveKeyword");
            };
        }
    });
};

//删除
function deleteKeyword(id) {
    layer.confirm("您确认删除选定的记录吗？", {
        btn: ["确定", "取消"]
    }, function () {
        $.ajax({
            type: "POST",
            url: "/Webmonitor/Keyword/DeleteById",
            data: { "id": id },
            success: function (data) {
                if (data.result == "Success") {
                    loadKeywordTables($("#KeywordType2Id2").val())
                    layer.closeAll();
                }
                else {
                    layer.alert("删除失败！");
                }
            }
        })
    });
};

